/*
 * SonarQube PDF Report
 * Copyright (C) 2010-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.report.pdf.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Utility class for reading custom configuration properties
 */
public class ConfigUtil {

    private static final String CUSTOM_CONFIG_FILE = "custom-pdf.properties";
    private static Properties properties = null;

    /**
     * Load custom configuration properties from file
     * 
     * @return Properties object containing custom configuration
     */
    private static Properties loadProperties() {
        Properties props = new Properties();
        try (InputStream input = ConfigUtil.class.getClassLoader().getResourceAsStream(CUSTOM_CONFIG_FILE)) {
            if (input != null) {
                props.load(input);
            } else {
                System.err.println("Custom configuration file not found: " + CUSTOM_CONFIG_FILE);
            }
        } catch (IOException e) {
            System.err.println("Error loading custom configuration: " + e.getMessage());
        }
        return props;
    }

    /**
     * Get custom property value as String
     * 
     * @param key the property key
     * @param defaultValue default value if property not found
     * @return property value or default value
     */
    public static String getProperty(String key, String defaultValue) {
        if (properties == null) {
            properties = loadProperties();
        }
        return properties.getProperty(key, defaultValue);
    }

    /**
     * Get custom property value as String
     * 
     * @param key the property key
     * @return property value or null if not found
     */
    public static String getProperty(String key) {
        return getProperty(key, null);
    }

    /**
     * Get custom property value as integer
     * 
     * @param key the property key
     * @param defaultValue default value if property not found or invalid
     * @return property value as integer
     */
    public static int getIntProperty(String key, int defaultValue) {
        String value = getProperty(key);
        if (value != null) {
            try {
                return Integer.parseInt(value.trim());
            } catch (NumberFormatException e) {
                System.err.println("Invalid integer value for property '" + key + "': " + value);
            }
        }
        return defaultValue;
    }

    /**
     * Get custom property value as boolean
     * 
     * @param key the property key
     * @param defaultValue default value if property not found
     * @return property value as boolean
     */
    public static boolean getBooleanProperty(String key, boolean defaultValue) {
        String value = getProperty(key);
        if (value != null) {
            return Boolean.parseBoolean(value.trim());
        }
        return defaultValue;
    }

    /**
     * Check if custom configuration file exists and is readable
     * 
     * @return true if configuration file exists and is readable
     */
    public static boolean isConfigAvailable() {
        if (properties == null) {
            properties = loadProperties();
        }
        return !properties.isEmpty();
    }

    /**
     * Reload configuration properties from file
     */
    public static void reloadConfig() {
        properties = loadProperties();
    }
}
